package com.easy.generator.java;

import com.easy.generator.Easy;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDate;

/**
 * @author easy
 * @date 2025年07月05日 19:36
 */
public class UtilGenerator {
    private static final String CLASS_NAME = "Util";
    private static final String PACKAGE_NAME = Easy.UTIL_PACKAGE;

    private static final String CLASS_TEMPLATE =
                    "package "+PACKAGE_NAME+";\n" +
                            "\n" +
                            "public class "+CLASS_NAME+" {\n" +
                            "    /**\n" +
                            "     * 是否为空\n" +
                            "     * @param objs\n" +
                            "     * @return   如果有一个为null就返回false,全不为null返回true\n" +
                            "     */\n" +
                            "    public static boolean isNull(Object... objs){\n" +
                            "        if(objs==null||objs.length==0){\n" +
                            "            return true;\n" +
                            "        }\n" +
                            "        for(Object obj:objs){\n" +
                            "            if(obj==null){\n" +
                            "                return true;\n" +
                            "            }\n" +
                            "        }\n" +
                            "        return false;\n" +
                            "    }\n" +
                            "\n" +
                            "    /**\n" +
                            "     * 是否为空串\n" +
                            "     * @param strs\n" +
                            "     * @return\n" +
                            "     */\n" +
                            "    public static boolean isEmpty(String... strs){\n" +
                            "        if(isNull(strs)){\n" +
                            "           return true;\n" +
                            "        }\n" +
                            "        for(String str:strs){\n" +
                            "            if(str.isEmpty()){\n" +
                            "                return true;\n" +
                            "            }\n" +
                            "        }\n" +
                            "        return false;\n" +
                            "    }\n" +
                            "}\n";
    public static void generateUtil() throws IOException {
        String content = String.format(CLASS_TEMPLATE, LocalDate.now());
        File outputFile = new File(Easy.PROJECT_ABSOLUTE_URL + Easy.SRC_DIR + Easy.convertPackagePath(PACKAGE_NAME) + "/" + CLASS_NAME + ".java");
        try (FileWriter writer = new FileWriter(outputFile)) {
            writer.write(content);
        }
    }
    public static void generate() throws IOException {
        Easy.initPackage(PACKAGE_NAME);
        generateUtil();
    }

}
